package com.netease.xone.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.netease.pkgRelated.XoneConsts.BUNDLE_KEY;
import com.netease.xone.R;
import com.netease.xone.app.XoneApp;
import com.netease.xone.notification.NotificationBarMgr;


public class FragUpdateApp extends FragmentBase implements OnClickListener{
    private Button mBUpdateOk;
    private Button mBUpdateCancel;
    private TextView mTvContent;
    private String mUrl, mContent, mVersion;
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View view = inflater.inflate(R.layout.fragment_update, container, false);
        findViews(view);
        return view;
    }
    private void findViews(View view){
        mBUpdateOk = (Button)view.findViewById(R.id.update_ok);
        mBUpdateCancel = (Button)view.findViewById(R.id.update_cancel);
        mTvContent = (TextView)view.findViewById(R.id.update_des);
        mBUpdateOk.setOnClickListener(this);
        mBUpdateCancel.setOnClickListener(this);
        
        Intent i = getActivity().getIntent();
        if(i != null){
            mUrl = i.getStringExtra(BUNDLE_KEY.UPDATE_URL);
            mContent = i.getStringExtra(BUNDLE_KEY.UPDATE_CONTENT);
            mVersion = i.getStringExtra(BUNDLE_KEY.UPDATE_VERSION);
            mTvContent.setText(mContent);
        }
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.update_ok:
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mUrl));
                startActivity(intent);
                getActivity().finish();
                break;
            case R.id.update_cancel:
                Bundle bundle = new Bundle();
                bundle.putString(BUNDLE_KEY.UPDATE_URL, mUrl);
                bundle.putString(BUNDLE_KEY.UPDATE_CONTENT, mContent);
                bundle.putString(BUNDLE_KEY.UPDATE_VERSION, mVersion);
                NotificationBarMgr.getInstance(XoneApp.getAppInstance()).showUpdateNotification(
                        R.drawable.icon_liked, System.currentTimeMillis(),
                        R.string.update_notification, bundle);
                getActivity().finish();
                break;
        }
    }
}
